<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        div {
            width: 100px;
            border: 1px rebeccapurple solid;
            border-radius: 5px;
            padding: 10px;
            margin: 0 auto;
        }
    </style>

</head>

<body>

    <div>

        全选<input type="checkbox">
        <hr>
        <input type="checkbox">选项一 <br>
        <input type="checkbox">选项二<br>
        <input type="checkbox">选项三<br>
        <input type="checkbox">选项四<br>


    </div>

    <script>
        var allBtn = document.querySelector('input')
        var items = document.querySelectorAll('input:nth-child(n + 2)')
        console.log(allBtn);
        console.log(items);

        allBtn.onclick = function () {
            var type = allBtn.checked

            console.log(type);
            for (var i = 0; i < items.length; i++) {
                items[i].checked = type
            }

            for (var j = 0; j < items.length; j++) {
                items[j].onclick = function () {
                    var flag = true
                    for (var c = 0; c < items.length; c++) {
                        if (items[c].checked == false) {
                            flag = false
                            break
                        }
                    }
                //    console.log(flag); 
                   allBtn.checked = flag
                }
            }

        }

    </script>

</body>

</html>